
const container = require('rhea');
//获取当前时间戳

var timestamp = Math.round(new Date());
//建立连接。
    var connection = container.connect({
    //接入域名，请参考这里。
    //'host': '${UUCID}.iot-amqps.cn-north-4.myhuaweicloud.com',
    'host': 'a4aa8cb3b7.iot-amqps.cn-north-4.myhuaweicloud.com',
    'port': 5671,
    'transport': 'tls',
    'reconnect': true,
    'idle_time_out': 8000,

    //userName组装方法，请参考这里。
   // 'username': 'accessKey=${yourAccessKey}|timestamp=' + timestamp + '|',
    'username': 'accessKey=ywgLHzR6|timestamp=' + timestamp + '|',
    //accessCode，请参考这里。
    //'password': '${yourAccessCode}',
    'password': 'yZFbipCMSt3FmULzdHpBee3qQwTYgsBJ',
    'saslMechannisms': 'PLAIN',
    'rejectUnauthorized': false,
    'hostname': 'default',
});
//创建Receiver连接。 队列名，可以使用默认队列DefaultQueue
//var receiver = connection.open_receiver('${yourQueue}');

var receiver = connection.open_receiver('DDDDDDDD');

var app={
    "resource": "device.property",
    "event": "report",
    "event_time": "string",
    "notify_data": {
      "header": {
        "app_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
        "device_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
        "node_id": "ABC123456789",
        "product_id": "ABC123456789",
        "gateway_id": "d4922d8a-6c8e-4396-852c-164aefa6638f",
        "tags": [
          {
            "tag_key": "testTagName",
            "tag_value": "testTagValue"
          }
        ]
      },
      "body": {
        "services": [
          {
            "service_id": "string",
            "properties": {
              "Temperature": 25,
              "Humidity": 52,
              "Luminance": 45,
              "LightStatus": "OFF",
              "MotorStatus": "OFF",
              "TQ": 10
            },
            "event_time": "string"
          }
        ]
      }
    }
  };
  var 温度;
  var 湿度;
  var 光照强度;
  var 灯开关;
  var 电机开关;
  var 下雨概率;
//接收云端推送消息的回调函数。
container.on('message', function (context) {
    var msg = context.message;
    var content = msg.body;
    var obj =JSON.parse(content)
  //  console.log(obj.notify_data.body.services[0].properties.Temperature);
  //  console.log(obj.notify_data.body.services[0].properties.Humidity);
  //  console.log(obj.notify_data.body.services[0].properties.Luminance);
  //  console.log(obj.notify_data.body.services[0].properties.LightStatus);
  //  console.log(obj.notify_data.body.services[0].properties.MotorStatus);
  //  console.log(obj.notify_data.body.services[0].properties.TQ);

 //温度 = obj.notify_data.body.services[0].properties.Temperature;
//  湿度 = obj.notify_data.body.services[0].properties.Humidity;
//  光照强度 = obj.notify_data.body.services[0].properties.Luminance;
//  灯开关 = obj.notify_data.body.services[0].properties.LightStatus;
//   电机开关 = obj.notify_data.body.services[0].properties.MotorStatus;
//  下雨概率 =obj.notify_data.body.services[0].properties.TQ;
 下雨概率 = JSON.stringify(obj.notify_data.body.services[0].properties.TQ);
 温度 = JSON.stringify(obj.notify_data.body.services[0].properties.Temperature);
 湿度 = JSON.stringify(obj.notify_data.body.services[0].properties.Humidity);
 光照强度 = JSON.stringify(obj.notify_data.body.services[0].properties.Luminance);
 灯开关 = JSON.stringify(obj.notify_data.body.services[0].properties.LightStatus);
 电机开关 = JSON.stringify(obj.notify_data.body.services[0].properties.MotorStatus);
    //发送ACK，注意不要在回调函数有耗时逻辑。
    context.delivery.accept();
});


var http =require("http");

http.createServer((req,res)=>{
  res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"})
  res.write(`<html>温度：</hrml>`+温度+`<html><br><html>`)
  res.write(`<html>湿度：</hrml>`+湿度+`<html><br><html>`)
  res.write(`<html>光照强度：</hrml>`+光照强度+`<html><br><html>`)
  res.write(`<html>电机开关：</hrml>`+电机开关+`<html><br><html>`)
  res.write(`<html>灯开关：</hrml>`+灯开关+`<html><br><html>`)
  res.write(`<html>下雨概率：</hrml>`+下雨概率+"%"+`<html><br><html>`)
  res.write(`<html><meta http-equiv="refresh" content="1"></hrml>`)
  
	res.end()
}).listen(3006,()=>{
	console.log("ok")
})

